package com.xqboss.apps.controller.game;



import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xqboss.apps.domain.game.GameMaterialStrikeRecord;
import com.xqboss.apps.dto.game.GameMaterialStrikePlayDto;
import com.xqboss.apps.dto.game.GameMaterialStrikeRecordDto;
import com.xqboss.apps.enums.Material.MaterialTypeEnum;
import com.xqboss.apps.service.game.GameMaterialStrikeRecordService;
import com.xqboss.apps.vo.game.materialStrike.GameMaterialStrikeIndexResultVO;
import com.xqboss.apps.vo.game.materialStrike.GameMaterialStrikePlayResultVO;
import com.xqboss.apps.vo.game.materialStrike.GameRankTopVo;
import com.xqboss.common.annotation.RepeatSubmit;
import com.xqboss.common.core.controller.BaseController;
import com.xqboss.common.core.domain.PageResult;
import com.xqboss.common.core.domain.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 材料冲击 前端控制器
 * </p>
 *
 * @author lujiahong
 * @since 2024-01-29
 */
@Slf4j
@RestController
@RequestMapping("/gameMaterialStrike/")
@Api(tags = "游戏-材料冲击相关接口")
public class GameMaterialStrikeController extends BaseController {

    @Resource
    private GameMaterialStrikeRecordService gameMaterialStrikeRecordService;

    @ApiOperation("材料冲击首页")
    @GetMapping(value = "index")
    public R<GameMaterialStrikeIndexResultVO> index() {
        try{
            GameMaterialStrikeIndexResultVO vo = gameMaterialStrikeRecordService.index(getUserId());
            return R.ok(vo);
        }catch (Exception e) {
            log.error("材料冲击首页异常：",e);
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("用户冲击记录")
    @GetMapping(value = "records")
    public PageResult<GameMaterialStrikeRecord> records(GameMaterialStrikeRecordDto<GameMaterialStrikeRecord> dto) {
        try{
            IPage<GameMaterialStrikeRecord> records = gameMaterialStrikeRecordService.records(dto, getUserId());
            return PageResult.ok(records);
        }catch (Exception e) {
            log.error("用户冲击记录异常：",e);
            return PageResult.fail(e.getMessage());
        }
    }

    @ApiOperation("冲击")
    @PostMapping(value = "play")
    @RepeatSubmit(interval = 3000,justUrl = true)
    public R<GameMaterialStrikePlayResultVO> play(@RequestBody GameMaterialStrikePlayDto dto) {
        try{
            return gameMaterialStrikeRecordService.play(dto, getUserId());
        }catch (Exception e) {
            log.error("冲击异常：",e);
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("消息播报")
    @GetMapping(value = "barrage")
    public R<List<com.alibaba.fastjson2.JSONObject>> barrage(@RequestParam(required = false) Long ts) {
        try{
            return R.ok(gameMaterialStrikeRecordService.barrage(ts));
        }catch (Exception e) {
            log.error("消息播报异常：",e);
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("投入排行榜")
    @GetMapping(value = "inputTop")
    public R<List<GameRankTopVo>> inputTop(@RequestParam MaterialTypeEnum type) {
        return R.ok(gameMaterialStrikeRecordService.inputTop(type));
    }
}
